package com.parkinglot.pojo;

import com.github.wxpay.sdk.WXPayUtil;
import com.parkinglot.util.HttpTools;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Getter
@Component
@Slf4j
public class WechatPay implements Serializable {
   private static final String appId = "wx9832ac19576d1e1b";
    private static final String mch_id = "1604456142";
    private static final  String key = "401458FD86EF48488D087B7BF1BF848C";

    public static String getAppId() {
        return appId;
    }

    public static String getMch_id() {
        return mch_id;
    }

    public static String getKey() {
        return key;
    }

    public static String paiparm(String ordernumber,String openid,String paymoney,int zt) throws Exception {

        //拼接下单地址参数
        Map<String, String> paraMap = new HashMap<String, String>();
        paraMap.put("appid", appId);
        paraMap.put("mch_id", mch_id);
        paraMap.put("nonce_str", WXPayUtil.generateNonceStr());
        paraMap.put("out_trade_no",ordernumber);
        paraMap.put("openid", openid);
        //paraMap.put("total_fee", String.valueOf(orderqueryVo.getPayMoney().multiply(new BigDecimal(100)).intValue()));//单位为分
        paraMap.put("total_fee", paymoney);
        paraMap.put("spbill_create_ip", "123.12.12.123");
        if(zt==0){

            paraMap.put("notify_url", "http://47.104.250.83/parking/pay/wxNotify");//微信支付回调
        }else if(zt==1){
            paraMap.put("notify_url", "http://47.104.250.83/parking/pay/wxmonthvipNotify");//月票支付回调
        }else if(zt==2){
            paraMap.put("notify_url", "http://47.104.250.83/parking/pay/wxmonthrenenwNotify");//月票续费回调
        }
        paraMap.put("trade_type", "JSAPI");
        paraMap.put("body", "测试微信支付");
        String sign = WXPayUtil.generateSignature(paraMap, key);
        paraMap.put("sign", sign);
        String xml = WXPayUtil.mapToXml(paraMap);
        String unifiedorder_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        Map<String, String> mapHeader = new HashMap<String, String>();
        mapHeader.put("Content-Type", "application/json;charset=utf-8");
        String xmlStr = HttpTools.post(unifiedorder_url, xml, mapHeader);
        System.out.println("调取微信支付返回的值：" + xmlStr);

        return xmlStr;
    }

    public static Map<String, String> issuccess(String xmlStr) throws Exception {
        Map<String, String> result=new HashMap<>();
        result.put("fire","支付失败");
        if (xmlStr.indexOf("SUCCESS") != -1) {
            Map<String, String> map = WXPayUtil.xmlToMap(xmlStr);
            Map<String, String> reData = new HashMap<>();
            reData.put("appId", appId);
            reData.put("nonceStr", map.get("nonce_str"));
            String newPackage = "prepay_id=" + map.get("prepay_id");
            reData.put("package", newPackage);
            reData.put("signType", "MD5");
            reData.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
            String newSign = WXPayUtil.generateSignature(reData, key);
            map.put("paySign", newSign);
            map.put("timeStamp", reData.get("timeStamp"));
          //  log.info("时间:"+ DateUtils.getFormatDate(new Date())+ ":openid:"+openid+"发起支付请求,支付金额:"+String.valueOf(orderqueryVo.getPayMoney().multiply(new BigDecimal(100)).intValue())+"停车场编号"+orderPayParm.getGarageid());
            return map;
        }

        return null;
    }

}
